package com.wn.servlet.req;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

/**
 * @author yellow Docter
 * @date 2022 -06-29
 * @desc
 *
 * @WebServlet(value = "/demo01") 注解可以写 value = "/demo01"
 * 如果注解是一个value属性设置值,value 可以省略
 * 直接写字符串进映射
 *
 *  获取请求参数的方法: String xxx = req.getParameter("xxx");
 *
 *  获取头信息 的方法: String accept = req.getHeader("Accept");
 *
 */
@WebServlet("/demo01")
public class Demo01Servlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

//        String xxx = req.getParameter("xxx");

        // 获取头 信息
        String accept = req.getHeader("Accept");
        System.out.println("accept = " + accept);
        String acceptLanguage = req.getHeader("Accept-Language");
        System.out.println("acceptLanguage = " + acceptLanguage);

        //获取所有的头信息
        // Enumeration 古老的迭代器
        // 判断元素的方法 hasMoreElements() / 获取元素的方法  nextElement()
        Enumeration<String> headerNames = req.getHeaderNames();

        while (headerNames.hasMoreElements()) {
            String s = headerNames.nextElement();
            System.out.println("头的名称为: = " + s);
            String s1 = req.getHeader(s);
            System.out.println("头的名称为内容: = " + s1);


        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}

